package com.sqx.modules.app.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserCertification;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.service.UserCertificationService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@AllArgsConstructor
@RequestMapping("/app/userCertification")
@Api("app实 名 认 证")
public class AppUserCertificationController {

    private UserCertificationService userCertificationService;
    private CommonInfoService commonInfoService;
    private UserMoneyService userMoneyService;


    /**
     * 实名认证
     **/
    @RequestMapping("/insert")
    @Login
    @ApiOperation("实名认证")
    public Result insert(@RequestAttribute Long userId, @ApiParam("真实姓名") String name, @ApiParam("身份证号") String idNumber,
                         @ApiParam("正面") String front, @ApiParam("反面") String back,Integer classify,String phone,
                         String address,String shopImg,String shopName,String addressImg,String startTime,String endTime,String shopType,String longitude,String latitude) {
        return userCertificationService.insert(userId, name, idNumber, front, back,classify,phone,address,shopImg,shopName,addressImg,startTime,endTime,shopType,longitude,latitude);
    }

    @Login
    @PostMapping("/updateCertification")
    @ApiOperation("修改师傅信息")
    public Result updateCertification(@RequestBody UserCertification userCertification){
        UserCertification userCertification1 = userCertificationService.getOne(new QueryWrapper<UserCertification>().eq("user_id", userCertification.getUserId()));
        userCertification.setId(userCertification1.getId());
        if(userCertification.getOnLine()==1){
            CommonInfo commonInfo = commonInfoService.findOne(616);
            UserMoney userMoney = userMoneyService.getOne(new QueryWrapper<UserMoney>().eq("user_id", userCertification1.getUserId()));
            if (userMoney.getSafetyMoney().compareTo(new BigDecimal(commonInfo.getValue())) < 0) {
                return Result.error(508,"当前用户未缴纳足额" + commonInfo.getValue() + "元的保证金");
            }
        }
        userCertificationService.updateById(userCertification);
        return Result.success();
    }

    /**
     * 实名认证
     **/
    @RequestMapping("/inserts")
    @Login
    @ApiOperation("师傅实名认证")
    public Result insert(@RequestBody UserCertification userCertification) {
        return userCertificationService.inserts(userCertification);
    }



    /**
     * 是否进行实名
     */
    @RequestMapping("/isInsert")
    @Login
    @ApiOperation("是否进行实名")
    public Result isInsert(@RequestAttribute Long userId) {
        return userCertificationService.isInsert(userId);
    }

//    /**
//     * 查询自己的实名信息
//     */
//    @RequestMapping("/queryInsert")
//    @Login
//    @ApiOperation("查询自己的实名信息")
//    public Result queryInsert(@RequestAttribute Long userId,Integer classify) {
//
//        return userCertificationService.queryInsert(userId,classify);
//    }
    /**
     * 查询自己的实名信息
     */
    @RequestMapping("/queryInsert")
    @Login
    @ApiOperation("查询自己的实名信息")
    public Result queryInsert(@RequestAttribute Long userId,String longitude,String latitude) {

        return userCertificationService.queryInsert(userId);
    }

    @GetMapping("/selectCertificationById")
    @ApiOperation("查询师傅信息")
    public Result selectCertificationById(Long id,String longitude,String latitude){
        return userCertificationService.selectCertificationById(id, longitude, latitude);
    }

    /**
     * 实名列表
     */
    @RequestMapping("/queryCertification")
    @ApiOperation("查询师傅或商家列表")
    public Result queryCertification(Long page, Long limit, @ApiParam("姓名") String name ,Integer classify,
                                     Integer isRecommend,Integer onLine,String city,Integer classifyId,
                                     String longitude,String latitude,String shopName ,String phone) {

        return userCertificationService.queryCertification(page, limit, "1", name, classify,isRecommend,
                onLine,city,classifyId,longitude,latitude,shopName,phone,1);
    }

}
